/* -+- file  ./kf2/Exposition/UltraTextView.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;

namespace Habazie.kf2.Exposition
{
	///<summary>Extended Text widget</summary>
	public class UltraTextView : Gtk.TextView
	{
		///<summary>User has pressed Enter key</summary>
		public event System.EventHandler Activated;
		
		public UltraTextView() : base ()
		{
			this.LeftMargin = 1;
			this.PixelsAboveLines = 1;
		}
		
		
		public UltraTextView(Gtk.TextBuffer buffer) : base (buffer)
		{
			this.LeftMargin = 1;
			this.PixelsAboveLines = 1;
		}
		
		
		///<summary>Adjust size of widget so it dysplays its contents without the need of scrolling</summary>
		public void ConfigureHeight ()
		{
			if (Parent != null) {
				Gtk.TextIter end, start;
				Buffer.GetBounds (out start, out end);
				Gdk.Rectangle rect = GetIterLocation (end);
				int height = rect.Height + rect.Y;
				Parent.SetSizeRequest (7, System.Math.Min (height + 4, 5*rect.Height + 9));
			}
		}
		
		protected override bool OnKeyPressEvent (Gdk.EventKey evnt)
		{
		//	Console.WriteLine ("kaka={0}", evnt.State);
			if (evnt.Key == Gdk.Key.Return && (evnt.State & Gdk.ModifierType.ShiftMask) != Gdk.ModifierType.ShiftMask) {
				if (Activated != null)
					Activated (this, new System.EventArgs ());
				return true;
			}
			bool ret =  base.OnKeyPressEvent (evnt);
			ConfigureHeight ();
			return ret;
		}
		
		
		protected override void OnCutClipboard ()
		{
			base.OnCutClipboard ();
			ConfigureHeight ();
		}
		
		
		protected override void OnPasteClipboard ()
		{
			base.OnPasteClipboard ();
			ConfigureHeight ();
		}
	}
	
}
